// pages/order_list/order_list.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderList: [],
    pageIndex: 1,
    isLoadAll:false,  //是否全部加载完毕
    onPay:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var stateId = options.id;
    this.setData({
      currentType: stateId
    })
    this.getOrder()
  },
  //订单详情
  getOrder: function () {
    var that = this;
    wx.request({
      url: 'http://111.230.44.126:88/api/v1/order/by_user',
      method: "get",
      data: {
        page: that.data.pageIndex
      },
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      success: function (res) {
        if (res.data.data.length > 0) {
          //进行合并数组，两个数组进行合并
          that.data.orderList.push.apply(that.data.orderList,res.data.data)
          that.setData({
            orderList: that.data.orderList
          })
        }
        else{
          //服务器中的数据已经全部加载完毕
          that.data.isLoadAll=true;
        }

      }
    })
  },
  //页面下拉加载
  onReachBottom:function(){
    if(!this.data.isLoadAll){
       this.data.pageIndex++;
       this.getOrder()
    }
  },
  showOrderDetailInfo:function(event){
    var id=event.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../to_pay/to_pay?from=order&id='+id,
    })
  },

  //直接进行支付
  rePay: function (event) {
    var orderNumber = event.currentTarget.dataset.id
    var that = this;
    wx.removeStorageSync('shopCar');
    if (!this.data.onPay) {
      wx.showModal({
        title: '支付提示',
        content: '本产品仅用于演示，支付系统已屏蔽',
        showCancel: false
      })
    }
    else {
      wx.request({
        url: 'http://111.230.44.126:88/api/v1/pay/pre_order',
        method: 'post',
        data: { id: orderNumber },
        header: {
          "content-type": "application/json",
          "token": wx.getStorageSync("token")
        },
        success: function (res) {
          var timeStamp = res.timeStamp;
          var statusCode = res.statusCode;
          if (timeStamp) { //可以支付
            wx.requestPayment({
              'timeStamp': timeStamp.toString(),
              'nonceStr': data.nonceStr,
              'package': data.package,
              'signType': data.signType,
              'paySign': data.paySign,
              success: function () {
                res.statusCode == 2;
              },
              fail: function () {
                res.statusCode == 1;
              }
            });
          }
          else {
            res.statusCode == 0
          }
          if (res.statusCode != 0) {
            statusCode == 2;
            wx.navigateTo({
              url: '../pay-result/pay-result?id=' + orderNumber + '&statusCode=' + statusCode + '&from=order',
            })
          }
        }
      })
    }
  }
})